package com.hscm.boss.mvc.controller;


import com.hscm.boss.mvc.model.AjaxResult;
import com.hscm.boss.mvc.model.dto.operator.LoginResponseDto;
import com.hscm.boss.mvc.model.from.operator.LoginFrom;
import com.hscm.boss.mvc.model.menus.SubMenu;
import com.hscm.boss.mvc.service.OperatorService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/operator") //公共的级别的地址
public class OperatorController {

    @Resource
    private OperatorService operatorService;

    /*登录获取Token*/

    @PostMapping("/login")
    public AjaxResult operatorLogin(@RequestBody @Valid LoginFrom loginFrom) {

        //进行登录生成JWT
        LoginResponseDto loginResponseDto = operatorService.OperatorLogin(loginFrom);
        //返回dto 给前端
        return AjaxResult.ok(loginResponseDto);
    }

    @GetMapping("/menus")
    public AjaxResult getMenus(@RequestHeader("operatorId") Long operatorId) {
        List<SubMenu> subMenuList = operatorService.getMenuByLoginUser(operatorId);
        return AjaxResult.ok(subMenuList);
    }

    /*退出系统*/
    @PostMapping("/logout")
    public AjaxResult logout(@RequestHeader("operatorId") Long operatorId) {
        //删除redis中Token中jwt信息  即删除key
        operatorService.OperatorLogout(operatorId);
        return AjaxResult.ok();
    }

}
